﻿using System;
using System.Windows.Forms;

namespace Scrobbler.UI.WF.Windows
{
    public partial class ConfigurationWindow : Form
    {
        private readonly IConfiguration _temp;
        private IConfiguration _configuration;


        public ConfigurationWindow(IConfiguration configuration)
        {
            _configuration = configuration;
            _temp = (IConfiguration) _configuration.Clone();
            InitializeComponent();
            Bind();
        }

        private void Bind()
        {
            enableLastfm.Checked = _configuration.UseLastfm;
            lastfmUsername.Text = _configuration.LastfmUsername;
            lastfmPassword.Text = _configuration.LastfmPassword;
            proxyAddress.Text = _configuration.ProxyAddress;
        }

        private void Save()
        {
            _configuration.UseLastfm = enableLastfm.Checked;
            _configuration.LastfmUsername = lastfmUsername.Text;
            _configuration.LastfmPassword = lastfmPassword.Text;
            _configuration.ProxyAddress = proxyAddress.Text;
            _configuration.Save(GlobalConfiguration.ConfigurationPath);
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            Save();
            Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            _configuration = (IConfiguration) _temp.Clone();
            Close();
        }
    }
}
